package com.zzsy.carosshopprojects.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.yatoooon.screenadaptation.ScreenAdapterTools;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.adapter.NumberKeyboardAdapter;
import com.zzsy.carosshopprojects.bean.NumberKeyboardBean;

import java.util.ArrayList;
import java.util.List;

public class MyNumberKeyboard extends LinearLayout {
    private MyGridView recycleView;
    private Button btnSure;
    private NumberKeyboardAdapter adapter;
    private List<NumberKeyboardBean> list = new ArrayList<>();
    private String[] str = {"1", "2", "3", "4", "5", "6", "7", "8", "9", ".", "0", "删除"};
    private KeyClickCallback mKeyClickCallback;

    public void setKeyClickCallback(KeyClickCallback callback) {
        this.mKeyClickCallback = callback;
    }

    public MyNumberKeyboard(Context context) {
        super(context);
        initView(context);
    }

    public MyNumberKeyboard(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public MyNumberKeyboard(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    void initView(Context context) {
        View v = LayoutInflater.from(context).inflate(R.layout.mynumberkeyboard, null);
        ScreenAdapterTools.getInstance().loadView(v);
        recycleView = v.findViewById(R.id.recycleView);
        btnSure = v.findViewById(R.id.btn_sure);
        btnSure.setOnClickListener(view -> {
            mKeyClickCallback.sure();
        });
        this.addView(v);
        for (int i = 0; i < str.length; i++) {
            NumberKeyboardBean bean = new NumberKeyboardBean();
            bean.setNum(str[i]);
            list.add(bean);
        }
        adapter = new NumberKeyboardAdapter(context, list, 0);
        recycleView.setAdapter(adapter);
        recycleView.setOnItemClickListener((adapterView, view, i, l) -> {
            if (i == 11) {
                mKeyClickCallback.onDelClick();
            } else {
                mKeyClickCallback.onNumClick(list.get(i).getNum());
            }

        });
    }

    public interface KeyClickCallback {

        /**
         * 数字按键被点击
         */
        void onNumClick(String keyNum);

        /**
         * 退格按键
         */
        void onDelClick();

        /**
         * 清空按键
         */
        void onCleanClick();

        void sure();
    }

}
